#include "stdafx.h"
#include "Time.h"
#include <time.h>
#include <sstream>
#include <string>

const int HOUR = 59;
const int DAY = 23;
const int MONTH = 30;
const int YEAR = 11;

using namespace std;

/*------------------------------------------------------------
/ Time
/ Description: default constructor
/ Parameters: N/A
/ Return: N/A
------------------------------------------------------------*/
Time::Time(){

}

/*------------------------------------------------------------
/ Time
/ Description: alternate constructor
/ Parameters: N/A
/ Return: N/A
------------------------------------------------------------*/
Time::Time(string _t){ 
	h = extractH(_t);
	m = extractM(_t);
}

/*------------------------------------------------------------
/ Time
/ Description: alternate constructor
/ Parameters: N/A
/ Return: N/A
------------------------------------------------------------*/
Time::Time(int _h, int _m){
	h = _h;
	m = _m;
	date = 0;
	month = 0;
	year = 0;
}

<<<<<<< .mine
Time::Time(int _m){
	h = 0;
	m = _m;
	date = 0;
	month = 0;
	year = 0;
}

Time::Time(int _h, int _m, int _date){
	h = _h;
	m = _m;
	date = _date;
	month = 0;
	year = 0;

}

Time::Time(int _h, int _m, int _date, int _month, int _year){
	h = _h;
	m = _m;
	date = _date;
	month = _month;
	year = _year;
}


=======
Time::Time(int _h, int _m, int _date, int _month, int _year){
	h = _h;
	m = _m;
	date = _date;
	month = _month;
	year = _year;
}

>>>>>>> .r38
/*------------------------------------------------------------
/ ~Time
/ Description: default destructor
/ Parameters: N/A
/ Return: N/A
------------------------------------------------------------*/
Time::~Time(){
}

/*------------------------------------------------------------
/ getH
/ Description: returns hour component of Time
/ Parameters: N/A
/ Return: int: hour component of Time
------------------------------------------------------------*/
int Time::getH(){
	return h;
}

/*------------------------------------------------------------
/ getM
/ Description: returns minute component of Time
/ Parameters: N/A
/ Return: int: minute component of Time
------------------------------------------------------------*/
int Time::getM(){
	return m;
}

int Time::getDate(){
	return date;
}

int Time::getMonth(){
	return month;
}

int Time::getYear(){
	return year;
}

/*------------------------------------------------------------
/ getTime
/ Description: returns a string representation of a Time
/ Parameters: N/A
/ Return: string: Time formatted as a string
------------------------------------------------------------*/
string Time::getTime(){
	std::stringstream buff;
	buff.str().erase();
	string s = "";
	if(h < 10){
		s.append("0");
	}	
	buff << h;
	s.append(buff.str());

	if(m < 10){
		s.append("0");
	}
	buff.str().erase();
	buff << m;
	s.append(buff.str());
	
	return s;
}

/*------------------------------------------------------------
/ setTime
/ Description: Generates a Time given a string representation
/ Parameters: _t: string representation of Time
/ Return: N/A
------------------------------------------------------------*/
void Time::setTime(string _t){
	h = extractH(_t);
	m = extractM(_t);
}

/*------------------------------------------------------------
/ setTime
/ Description: Generates a Time given integer representations
/				of the hour and minute components.
/ Parameters: _h: hour component of Time
/			  _t: minute component of Time
/ Return: N/A
------------------------------------------------------------*/
void Time::setTime(int _h, int _m){
	h = _h;
	m = _m;
}

/*------------------------------------------------------------
/ setH
/ Description: modifies a Time's hour
/ Parameters: _h: value for Time's hour component
/ Return: N/A
------------------------------------------------------------*/
void Time::setH(int _h){
	h = _h;
}

/*------------------------------------------------------------
/ setM
/ Description: modifies a Time's minute
/ Parameters: _m: value for Time's minute component
/ Return: N/A
------------------------------------------------------------*/
void Time::setM(int _m){
	m = _m;
}

void Time::setDate(int _date){
	date = _date;
}

void Time::setMonth(int _month){
	month = _month;
}

void Time::setYear(int _year){
	year = _year;
}

/*------------------------------------------------------------
/ checkIfLegal
/ Description: ensures a Time's hour and minute components are
/				valid values of a 24-hour clock
/ Parameters: N/A
/ Return: int: nonzero if Time is not valid; 0 if Time is valid
------------------------------------------------------------*/
int Time::checkIfLegal(){
	int trip = 1;
	if((h < 0) || (h > 23)){
		trip = 0;
	}
	if((m < 0) || (m > 59)){
		trip = 0;
	}
	return trip;
}

/*------------------------------------------------------------
/ fix
/ Description: translates invalid hour/minute attribute values
/				into valid values
/ Parameters: N/A
/ Return: int:
------------------------------------------------------------*/
int Time::fix(){
	int days = 0;
	h += m/60;
	m = m%60;
	days = h/24;
	h = h%24;
	return days;
}

/*------------------------------------------------------------
/ addTime
/ Description: extracts Time from string and adds it to this Time.
/				Calculates the number of days adding the given time
/				to this time results in
/ Parameters: _t: Time in string format ("00:00") to be added
/ Return: int: number of days ahead adding given Time results in
------------------------------------------------------------*/
int Time::addTime(string _t){
	h += extractH(_t);
	m += extractM(_t);

	if(!checkIfLegal()){
		return fix();
	}
	return 0;
}

/*------------------------------------------------------------
/ addTime
/ Description: adds the given hour and minute values to this Time.
/ Parameters: _h: number of hours to be added
/			  _m: number of minutes to be added
/ Return: int: number of days ahead adding given Time results in
------------------------------------------------------------*/
int Time::addTime(int _h, int _m){
	h += _h;
	m += _m;
	if(!checkIfLegal()){
		return fix();
	}
	return 0;
}

/*------------------------------------------------------------
/ addTime
/ Description: determines the combination of hours/minutes the
/				given value is composed of and adds it to this Time
/ Parameters: _m: number of minutes being added
/ Return: int: number of days ahead adding given Time results in
------------------------------------------------------------*/
int Time::addTime(int _m){
	h += _m/60;
	m += _m%60;
	if(!checkIfLegal()){
		return fix();
	}
	return 0;
}

/*------------------------------------------------------------
/ addTime
/ Description: adds two Time values together
/ Parameters: N/A
/ Return: int: number of days ahead adding given Time results in
------------------------------------------------------------*/
int Time::addTime(Time _t){
	h += _t.getH();
	m += _t.getM();
	if(!checkIfLegal()){
		return fix();
	}
	return 0;
}

/*------------------------------------------------------------
/ subtractTime
/ Description: finds result of taking difference between this
/				Time and the Time given in string format
/ Parameters: _t: string representation of Time being subtracted from this Time
/ Return: int: number of days in the past subtracting given Time results in
------------------------------------------------------------*/
int Time::subtractTime(string _t){
	h -= extractH(_t);
	m -= extractM(_t);
	if(!checkIfLegal()){
		return fix();
	}
	return 0;

}

/*------------------------------------------------------------
/ subtractTime
/ Description: finds result of taking difference between this
/				Time and the Time given as hour/minute values
/ Parameters: _h: hour component of Time being subtracted
/			  _m: minute component of Time being subtracted
/ Return: int: number of days in the past subtracting given Time results in
------------------------------------------------------------*/
int Time::subtractTime(int _h, int _m){
	h -= _h;
	m -= _m;
	if(!checkIfLegal()){
		return fix();
	}
	return 0;
}

/*------------------------------------------------------------
/ subtractTime
/ Description: finds result of taking difference between this
/				Time and the Time given in minutes
/ Parameters: _m: number of minutes being subtracted from this Time
/ Return: int: number of days in the past subtracting given Time results in
------------------------------------------------------------*/
int Time::subtractTime(int _m){
	h -= _m/60;
	m -= _m%60;
	if(!checkIfLegal()){
		return fix();
	}
	return 0;

}

/*------------------------------------------------------------
/ extractH
/ Description: finds the hour component given a Time's string representation
/ Parameters: _t: Time in string format ("00:00")
/ Return: int: hour component of given Time
------------------------------------------------------------*/
int Time::extractH(string _t){
	string substr = "";
	substr = _t.substr(0, _t.find_first_of(":"));
	return atoi(substr.c_str());
}

/*------------------------------------------------------------
/ extractM
/ Description: finds the minute component given a Time's string representation
/ Parameters: _t: Time in string format ("00:00")
/ Return: int: minute component of given Time
------------------------------------------------------------*/
int Time::extractM(string _t){
	string substr = "";
	substr = _t.substr(_t.find_first_of(":"), (_t.size()-1));
	return atoi(substr.c_str());
}
<<<<<<< .mine


/*------------------------------------------------------------
/ operator <
/ Description: determines whether the left hand side is less than the right hand side
/ Parameters: takes a Time pointer as the right hand side, and compares it to this
/ Return: returns 1 if the right hand side is larger than this
		  returns 0 if the right hand side is the less than or equal to this
------------------------------------------------------------*/
int Time::operator<(Time *_t){
	if (year < _t->getYear()){
		return 1;
	}else if (year == _t->getYear()){
		if (month < _t->getMonth()){
			return 1;
		}else if (month == _t->getMonth()){
			if (date < _t->getDate()){
				return 1;
			}else if (date == _t->getDate()){
				if (h < _t->getH()){
					return 1;
				}else if (h == _t->getH()){
					if (m < _t->getM()){
						return 1;
					}else 
						return 0;
				}
			}
		}
	}
	return 0;
}




/*------------------------------------------------------------
/ operator >
/ Description: determines whether the left hand side is greater than the right hand side
/ Parameters: takes a Time pointer as the right hand side, and compares it to this
/ Return: returns 0 if the right hand side is larger than this
		  returns 1 if the right hand side is the less than or equal to this
------------------------------------------------------------*/
int Time::operator>(Time *_t){
    
   	if (year < _t->getYear()){
		return 0;
	}else if (year == _t->getYear()){
		if (month < _t->getMonth()){
			return 0;
		}else if (month == _t->getMonth()){
			if (date < _t->getDate()){
				return 0;
			}else if (date == _t->getDate()){
				if (h < _t->getH()){
					return 0;
				}else if (h == _t->getH()){
					if (m < _t->getM()){
						return 0;
					}else 
						return 1;
				}
			}
		}
	}
	return 1;

}
/*------------------------------------------------------------
/ operator ==
/ Description: determines whether the left hand side is greater than the right hand side
/ Parameters: takes a Time pointer as the right hand side, and compares it to this
/ Return: returns 1 if the right hand side equals this
		  returns 0 any difference exists between the right hand side and this
------------------------------------------------------------*/
int Time::operator==(Time *_t){
	if((year == _t->getYear())&&(month == _t->getMonth())&&(date == _t->getDate())&&(h == _t->getH())&&(m == _t->getM())){
		return 1;
	}

	return 0;

}


/*------------------------------------------------------------
/ operator +
/ Description: adds the right hand side to this
/ Parameters: takes a Time pointer as the right hand side, and adds it to this
/ Return: a pointer to the sum of the time times
------------------------------------------------------------*/
Time* Time::operator+(Time *_t){
	Time* sum = NULL;
	int carry = 0;

	sum->setM(m + _t->getM()%HOUR);
	carry = (m + _t->getM())/HOUR;
	
	sum->setH(h + _t->getH()%DAY);
	carry = (h + _t->getH() + carry)/DAY;
	
	sum->setDate(date + _t->getDate()%MONTH);
	carry = (date + _t->getDate() + carry)/MONTH;
	
	sum->setMonth(month + _t->getMonth()%YEAR);
	carry = (month + _t->getMonth() + carry)/YEAR;
	
	sum->setYear(year + carry);
	return sum;
}



/*------------------------------------------------------------
/ operator clone
/ Description: returns a pointer to a new Time object having same member variable data as this
/ Parameters: none
/ Return: a pointer to a new Time object
------------------------------------------------------------*/
Time* Time::clone(){
	Time* newTime = NULL;
	newTime->setM(m);
	newTime->setH(h);
	newTime->setDate(date);
	newTime->setMonth(month);
	newTime->setYear(year);
	return newTime;
}=======

/*------------------------------------------------------------
/ compareTimes
/ Description: compares 2 times to determine which is bigger
/ Parameters: _t: Time1, time2
/ Return: int: identifier to bigger time
/		   less than 0 -- time1 < time2
/			equal to 0 -- time1 = time2
/		greater than 0 -- time1 > time2

/ Earlier is less than
------------------------------------------------------------*/
int Time::compareTimes(Time *_t2){
    
    if(h <  _t2->getH()){
        return -1;    //_t is larger than this
    }
    else if(h > _t2->getH()){
        return 1; //this is larger than _t
    }
    else{    //hours are the same, now look and see what the difference is between the minutes
        if(m < _t2->getM()){
            return -1;    //_t is larger than this
        }
        else if(m > _t2->getM()){
            return 1;    //this is larger than _t
        }
        else
            return 0;    //this and _t have no differences in hours and minutes
    }

}

>>>>>>> .r38
